<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="id" />
      <el-table-column label="商品数量" align="center" prop="quantity" />
      <el-table-column label="折扣" align="center" prop="discount">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sale_discount"
            :value="scope.row.discount"
          />
        </template>
      </el-table-column>
      <el-table-column label="合计" align="center" prop="amount" />
      <el-table-column label="应收" align="center" prop="receivable" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.order_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-zoom-in"
            @click="handleUpdate(scope.row, true)"
            v-hasPermi="['sale:info:query']"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="订单编号" prop="id">
              {{ form.id }}
            </el-form-item></el-col
          >
          <el-col :span="12">
            <el-form-item label="商品数量" prop="quantity">
              {{ form.quantity }}
            </el-form-item></el-col
          >
          <el-col :span="12">
            <el-form-item label="折扣" prop="discount">
              <dict-tag
                :options="dict.type.sale_discount"
                :value="form.discount"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="合计" prop="amount">
              {{ form.amount }}
            </el-form-item></el-col
          >
          <el-col :span="12">
            <el-form-item label="应收" prop="receivable">
              {{ form.receivable }}
            </el-form-item></el-col
          >
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              {{ form.remark }}
            </el-form-item></el-col
          >
          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <dict-tag
                :options="dict.type.order_status"
                :value="form.status"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-divider content-position="center">订单详细信息</el-divider>
        <el-table
          :data="saleDetailList"
          :row-class-name="rowSaleDetailIndex"
          @selection-change="handleSaleDetailSelectionChange"
          ref="saleDetail"
        >
          <el-table-column label="编号" prop="skuId"> </el-table-column>
          <el-table-column label="商品" prop="skuName">
            <template slot-scope="scope">
              <image-preview :src="scope.row.imgUrl" :width="50" :height="50" />
              {{ scope.row.skuName }}
            </template>
          </el-table-column>
          <el-table-column label="颜色" prop="color">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.sku_color"
                :value="scope.row.color"
              />
            </template>
          </el-table-column>
          <el-table-column label="尺码" prop="size">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sku_size" :value="scope.row.size" />
            </template>
          </el-table-column>
          <el-table-column label="价格" prop="skuPrice"> </el-table-column>
          <el-table-column label="数量" prop="quantity"> </el-table-column>
          <el-table-column label="小计" prop="detailPrice"> </el-table-column>
          <el-table-column label="折后价" prop="discountPrice" width="120">
            <template slot-scope="scope">
              <el-row>
                <el-col :span="12"> {{ scope.row.discountPrice }}</el-col>
                <el-col  :span="12">
                  <dict-tag
                    v-if="form.status == '2' && scope.row.isReturn != '0'"
                    :options="dict.type.return_status"
                    :value="scope.row.isReturn"
                  />
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-info"
                    v-if="form.status == '2' && scope.row.isReturn == '0'"
                    @click="handleReturn(scope.row)"
                    v-hasPermi="['sale:info:query']"
                    >退货</el-button
                  ></el-col
                >
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div v-if="!readFlag" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <remark
      ref="remarkDialog"
      :visible.sync="remarkOpen"
      :title="remarkTitle"
      @confirm="confirmReturn"
    ></remark>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
} from "@/api/sale/info";
import { addRefund } from "@/api/sale/refund";
import remark from "./remark.vue";

export default {
  name: "Info",
  dicts: [
    "sale_discount",
    "order_status",
    "sku_color",
    "sku_size",
    "return_status",
  ],
  components: { remark },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSaleDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      infoList: [],
      // 订单详细表格数据
      saleDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        quantity: [
          { required: true, message: "商品数量不能为空", trigger: "blur" },
        ],
        discount: [
          { required: true, message: "折扣不能为空", trigger: "change" },
        ],
        amount: [{ required: true, message: "合计不能为空", trigger: "blur" }],
        receivable: [
          { required: true, message: "应收不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
        ],
      },
      readFlag: false,
      remarkOpen: false,
      remarkTitle: "1111",
      returnForm: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleReturn(row) {
      this.remarkTitle = "退货";
      this.returnForm = row;
      this.remarkOpen = true;
    },
    confirmReturn(remark) {
      const infoId = this.form.id;
      const detailId = this.returnForm.detailId;
      const discountPrice = this.returnForm.discountPrice;
      this.$modal
        .confirm("是否确认退货？")
        .then(function () {
          return addRefund({
            infoId: infoId,
            detailId: detailId,
            reason: remark,
            refundAmount: discountPrice,
          });
        })
        .then(() => {
          this.returnForm.isReturn = "1";
          this.$modal.msgSuccess("操作成功");
        })
        .catch(() => {});
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        quantity: null,
        discount: null,
        amount: null,
        receivable: null,
        remark: null,
        status: null,
        createTime: null,
        updateTime: null,
      };
      this.saleDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.readFlag = false;
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row, readFlag = false) {
      this.reset();
      this.readFlag = readFlag;
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        this.saleDetailList = response.data.saleDetailList;
        this.open = true;
        if (readFlag) {
          this.title = "订单详细";
        } else {
          this.title = "修改订单";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.saleDetailList = this.saleDetailList;
          if (this.form.id != null) {
            updateInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除订单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 订单详细序号 */
    rowSaleDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 订单详细添加按钮操作 */
    handleAddSaleDetail() {
      let obj = {};
      obj.skuName = "";
      obj.imgUrl = "";
      obj.color = "";
      obj.size = "";
      obj.skuPrice = "";
      obj.quantity = "";
      obj.detailPrice = "";
      this.saleDetailList.push(obj);
    },
    /** 订单详细删除按钮操作 */
    handleDeleteSaleDetail() {
      if (this.checkedSaleDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的订单详细数据");
      } else {
        const saleDetailList = this.saleDetailList;
        const checkedSaleDetail = this.checkedSaleDetail;
        this.saleDetailList = saleDetailList.filter(function (item) {
          return checkedSaleDetail.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleSaleDetailSelectionChange(selection) {
      this.checkedSaleDetail = selection.map((item) => item.index);
    },
  },
};
</script>
